package com.txby.wxyyApi.component.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.MyBatisReflectUtil;
import com.jck.tools.ChkUtil;
import com.jck.tools.MD5Util;
import com.jck.tools.SERVER_ERR;
import com.txby.wxyyApi.data.CommonApiModelMapExt;
import com.txby.wxyyApi.entity.WrSysApiUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 安全令牌相关
* @author 作者 jacking
* @date 2017年6月22日 上午10:37:26
 */
@SuppressWarnings("all")
@RestController
@Api(value="安全令牌相关",description="安全令牌相关")
@RequestMapping("/wr/api")
public class WrSysApiUserVerifyController extends CommonController{
	private static Logger log = Logger.getLogger(WrSysApiUserVerifyController.class);
	
	@ApiOperation(value = "服务注册或更新", notes = "服务注册或更新")
	@PostMapping("/signIn/{type}/{userName}/{passWord}/{updateSign}")
	public Map signIn(@PathVariable("type") String type,@PathVariable("userName") final String userName,@PathVariable("passWord") String passWord,
			@PathVariable("updateSign") String updateSign,String oldPassWord) {
		Map map=new CommonApiModelMapExt();
		WrSysApiUser gau=null;
		String md5UpdateSign=MD5Util.getMD5ofStr(userName+passWord, 2);
		System.out.println(propertySourceConfig.getIsProMode());
		if(propertySourceConfig.getIsProMode()!=null&&propertySourceConfig.getIsProMode().equals("true")&&!md5UpdateSign.equals(updateSign)){
			return new CommonApiModelMapExt(SERVER_ERR.CODE_1.getCode(),"安全标识验证不通过","安全标识验证不通过");
		}else{
			Map paratmers=new HashMap<>();
			paratmers.put("userName", userName);
			gau=(WrSysApiUser) this.mBaseDao.findBySqlUniqueObject("select * from "+MyBatisReflectUtil.tablename(WrSysApiUser.class)+" where userName=#{pojo.userName} ", WrSysApiUser.class,paratmers 
			);
		}
		if(gau!=null) {
			if(!ChkUtil.isEmpty(oldPassWord)&&oldPassWord.equals(gau.getPassWord())){
				gau.setPassWord(MD5Util.getMD5ofStr(passWord, 2));
				this.mBaseDao.updateObject(gau);
				map= getCreateTokenByGzz01ApiUserAndPutCache(gau);
				map.put(CommonApiModelMapExt.INFO, "用户已存在,更新成功请牢记新的密码");
				map.put(CommonApiModelMapExt.MSG, "用户已存在,更新成功请牢记新的密码");
			}else{
				map =new CommonApiModelMapExt(SERVER_ERR.CODE_1.getCode(),"用户已存在，旧密码不正确或旧密码为空","用户已存在，旧密码不正确或旧密码为空");
			}
			
		}else{
			
			if(userName.startsWith("api_")){
				gau=new WrSysApiUser();
				gau.setUserName(userName);
				gau.setPassWord(MD5Util.getMD5ofStr(passWord, 2));
				this.mBaseDao.saveObject(gau);
				map= getCreateTokenByGzz01ApiUserAndPutCache(gau);
				map.put(CommonApiModelMapExt.INFO, "注册成功,用户名不存进行增加");
				map.put(CommonApiModelMapExt.MSG, "注册成功");
			}else{
				map =new CommonApiModelMapExt(SERVER_ERR.CODE_1.getCode(),"用户名称不合法","用户名称不合法");
			}
		}
		return map;
	}
	
	@ApiOperation(value = "服务验证", notes = "服务验证")
	@GetMapping("/verifyCheck/{type}/{userName}/{passWord}/{updateSign}")
	public Map verifyCheck(@PathVariable("type") String type,@PathVariable("userName") final String userName,@PathVariable("passWord") String passWord,
			@PathVariable("updateSign") String updateSign) {
		Map map=new CommonApiModelMapExt();
		WrSysApiUser gau=null;
		String md5UpdateSign=MD5Util.getMD5ofStr(userName+passWord, 2);
		if(propertySourceConfig.getIsProMode()!=null&&propertySourceConfig.getIsProMode().equals("true")&&!md5UpdateSign.equals(updateSign)){
			return new CommonApiModelMapExt(SERVER_ERR.CODE_1.getCode(),"安全标识验证不通过","安全标识验证不通过");
		}else{
		/*	Map paratmers=new HashMap<>();
			paratmers.put("userName", userName);*/
//			gau=(Gzz01ApiUser) this.mBaseDao.findBySqlUniqueObject("select * from "+MyBatisReflectUtil.tablename(Gzz01ApiUser.class)+" where userName=#{pojo.userName} ", Gzz01ApiUser.class,paratmers);
			gau=(WrSysApiUser) this.mBaseDao.findBySqlUniqueObject("select * from "+MyBatisReflectUtil.tablename(WrSysApiUser.class)+" where userName=#{pojo.userName} ", WrSysApiUser.class, new HashMap(){{
				put("userName", userName);
			}});
		}
		if(gau!=null) {
			if(passWord.equals(gau.getPassWord())){
				map= getCreateTokenByGzz01ApiUserAndPutCache(gau);
				map.put(CommonApiModelMapExt.INFO, "用户服务验证成功");
				map.put(CommonApiModelMapExt.MSG, "用户服务验证成功");
			}else{
				map =new CommonApiModelMapExt(SERVER_ERR.CODE_1.getCode(),"用户已存在，密码不正确","用户已存在，密码不正确");
			}
		}else{
			map=writeReturnJson(SERVER_ERR.CODE_1.getCode(), "用户不存在", "用户不存在");
		}
		return map;
	}
	

}